# 
# Description:
# This script constructs Tables C1-C8
# for Appendix C
#
# Requires: terror_pres_wide.dta, terror_house_wide, terror_senate_wide
#
# Author: Sean Nossek
#
# Date: August 04, 2020

# Load Packages

if (!require("pacman")) install.packages("pacman")

#lfe version manage
packageurl <- "https://cran.r-project.org/src/contrib/Archive/lfe/lfe_2.8.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(lfe)

pacman::p_load(readxl,
               tidyverse,
               readstata13,
               stargazer,
               margins,
               lubridate,
               gridExtra)

# set wd

setwd("C:/Users/Sean/Dropbox/Eran/Terrorism_Left/Paper/Revisions/Revised Paper/Replication/Paper Data")

# load attack level pres election data

pres_wide <- read.dta13("terror_pres_wide.dta") %>%
  mutate(two_party_share = rep_perc_vote_t2 + dem_perc_vote_t2,
         rep_tp_share = (rep_perc_vote_t2 / two_party_share) * 100,
         success_unknown = as.numeric(success == 1 & motive1 == "Unknown"),
         success_political = as.numeric(success == 1 & motive1 == "Political"),
         success_hatred = as.numeric(success == 1 & motive1 == "Hatred"),
         success_abortion = as.numeric(success == 1 & motive1 == "Anti-Abortion"),
         election_year = year(election_date_t2)) %>% 
  filter(motive1 != "", motive1 != "Religious")

# code incumbency

pres_wide$incumbent[pres_wide$election_year == 1972] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1976] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1980] <- "D"
pres_wide$incumbent[pres_wide$election_year == 1984] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1988] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1992] <- "R"
pres_wide$incumbent[pres_wide$election_year == 1996] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2000] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2004] <- "R"
pres_wide$incumbent[pres_wide$election_year == 2008] <- "R"
pres_wide$incumbent[pres_wide$election_year == 2012] <- "D"
pres_wide$incumbent[pres_wide$election_year == 2016] <- "D"

pres_wide$incumbent_rep <- as.numeric(pres_wide$incumbent == "R")

pres_wide$GeoFIPS <- str_pad(pres_wide$FIPSCounty,5,side = "left", "0")


#Table C1

pres_wide$state_fips <- as.factor(pres_wide$state_fips)
pres_wide$election_year <- as.factor(pres_wide$election_year)

mod1<- felm(rep_tp_share ~ success | year + FIPSCounty  + state_fips:election_year | 0 | FIPSCounty, data = pres_wide)
mod2<- felm(rep_tp_share ~ success | year + FIPSCounty  + state_fips:election_year | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 270))
mod3<- felm(rep_tp_share ~ success | year + FIPSCounty  + state_fips:election_year | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 180))
mod4<- felm(rep_tp_share ~ success | year + FIPSCounty  + state_fips:election_year | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table C2


pres_wide$rep_perc_vote_t2 <- pres_wide$rep_perc_vote_t2 * 100

mod1<- felm(rep_perc_vote_t2 ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide)
mod2<- felm(rep_perc_vote_t2 ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 270))
mod3<- felm(rep_perc_vote_t2 ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 180))
mod4<- felm(rep_perc_vote_t2 ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Percentage Vote", dep.var.caption = "", title = "Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table C3

pres_dem <- pres_wide %>%
  filter(incumbent_rep == 0)
pres_rep <- pres_wide %>%
  filter(incumbent_rep == 1)

mod1<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_dem)
mod2<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_rep)

stargazer(mod1, mod2, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "County and Year FEs, County Clustered SEs",
          column.labels = c("Democrat Incumbent","Republican Incumbent"))

# Table C4

pres_wide$success_nkill <- ifelse(pres_wide$success == 0, 0, ifelse(pres_wide$nkill == 0, NA, pres_wide$nkill))

mod1<- felm(rep_tp_share ~ success_nkill | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide)
mod2<- felm(rep_tp_share ~ success_nkill | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 270))
mod3<- felm(rep_tp_share ~ success_nkill | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 180))
mod4<- felm(rep_tp_share ~ success_nkill | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide, subset = (event_to_t2 <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table C5


pres_wide_sub <- pres_wide %>%
  filter(year_t2 <= 2000)

mod1<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub)
mod2<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 270))
mod3<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 180))
mod4<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table C6

pres_wide_sub <- pres_wide %>%
  filter(year_t2 > 2000)

mod1<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub)
mod2<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 270))
mod3<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 180))
mod4<- felm(rep_tp_share ~ success | year + FIPSCounty | 0 | FIPSCounty, data = pres_wide_sub, subset = (event_to_t2 <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table C7

house <- read.dta13("terror_house_wide.dta") %>%
  mutate(two_party_share = dem_perc_t2 + rep_perc_t2, rep_tp_share = (rep_perc_t2 / two_party_share) * 100)

house$trend <- as.numeric(house$year - min(house$year))
house$FIPSCounty <- as.factor(house$FIPSCounty)
house$year <- as.factor(house$year)

mod1 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = house)
mod2 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = house, subset = (event_to_t2 <= 270))
mod3 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = house, subset = (event_to_t2 <= 180))

stargazer(mod1, mod2, mod3, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months"))

# Table C8

senate <- read.dta13("terror_senate_wide.dta") %>%
  mutate(two_party_share = dem_perc_t2 + rep_perc_t2, rep_tp_share = (rep_perc_t2 / two_party_share) * 100)

senate$trend <- as.numeric(senate$year - min(senate$year))
senate$FIPSCounty <- as.factor(senate$FIPSCounty)
senate$year <- as.factor(senate$year)

mod1 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = senate)
mod2 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = senate, subset = (event_to_t2 <= 270))
mod3 <- felm(rep_tp_share ~ success | FIPSCounty + year | 0 | FIPSCounty, data = senate, subset = (event_to_t2 <= 180))

stargazer(mod1, mod2, mod3, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months"))